package com.xmut.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmut.eduservice.entity.EduCourse;
import com.xmut.eduservice.entity.EduCourseDescription;
import com.xmut.eduservice.entity.vo.CourseInfoVo;
import com.xmut.eduservice.entity.vo.CoursePublishVo;
import com.xmut.eduservice.entity.vo.CourseQuery;
import com.xmut.eduservice.mapper.EduCourseMapper;
import com.xmut.eduservice.service.EduChapterService;
import com.xmut.eduservice.service.EduCourseDescriptionService;
import com.xmut.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xmut.eduservice.service.EduVideoService;
import com.xmut.servicebase.exceptionhandler.GuliException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author huangchaoqin
 * @since 2020-08-04
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {
    @Autowired
    private EduCourseDescriptionService eduCourseDescriptionService;
    @Autowired
    private EduVideoService eduVideoService;
    @Autowired
    private EduChapterService eduChapterService;

    @Override
    public String saveCourseInfo(CourseInfoVo courseInfoVo) {

        // 首先将courseInfoVo分别导出到EduCourse和EduCourseDescription
        EduCourse eduCourse = new EduCourse();
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        BeanUtils.copyProperties(courseInfoVo, eduCourse);
        BeanUtils.copyProperties(courseInfoVo, eduCourseDescription);
        // 1 向课程表添加信息
        boolean save = this.save(eduCourse);
        if (!save) {
            throw new GuliException(20001, "添加课程失败");
        }

        String id = eduCourse.getId();
        eduCourseDescription.setId(id);
        // 2 课程描述表添加信息
        save = eduCourseDescriptionService.save(eduCourseDescription);
        if (!save) {
            throw new GuliException(20001, "添加课程失败");
        }
        return id;
    }

    @Override
    public CourseInfoVo getCourseInfo(String courseId) {
        CourseInfoVo courseInfoVo = new CourseInfoVo();
        // 1. 查询课程表
        EduCourse eduCourse = this.getById(courseId);
        BeanUtils.copyProperties(eduCourse, courseInfoVo);
        // 2. 查询简介表
        EduCourseDescription eduCourseDescription = eduCourseDescriptionService.getById(courseId);
        BeanUtils.copyProperties(eduCourseDescription, courseInfoVo);
        return courseInfoVo;
    }

    @Override
    public void updateCourseInfo(CourseInfoVo courseInfoVo) {
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo, eduCourse);
        boolean update1 = this.updateById(eduCourse);

        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        BeanUtils.copyProperties(courseInfoVo, eduCourseDescription);
        boolean update2 = eduCourseDescriptionService.updateById(eduCourseDescription);

        if (!update1 || !update2) {
            throw new GuliException(20001, "修改课程信息失败");
        }
    }

    @Override
    public CoursePublishVo getCoursePublishVoById(String id) {

        return baseMapper.getCoursePublishVoById(id);
    }

    @Override
    public void pageQuery(Page<EduCourse> pageParam, CourseQuery courseQuery) {
        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("gmt_create");
        if (courseQuery == null){
            baseMapper.selectPage(pageParam, queryWrapper);
            return;
        }
        String title = courseQuery.getTitle();
        String teacherId = courseQuery.getTeacherId();
        String subjectParentId = courseQuery.getSubjectParentId();
        String subjectId = courseQuery.getSubjectId();
        if (!StringUtils.isEmpty(title)) {
            queryWrapper.like("title", title);
        }
        if (!StringUtils.isEmpty(teacherId) ) {
            queryWrapper.eq("teacher_id", teacherId);
        }
        if (!StringUtils.isEmpty(subjectParentId)) {
            queryWrapper.eq("subject_parent_id", subjectParentId);
        }
        if (!StringUtils.isEmpty(subjectId)) {
            queryWrapper.eq("subject_id", subjectId);
        }
        baseMapper.selectPage(pageParam, queryWrapper);
    }

    @Override
    public boolean removeCourse(String courseId) {

        // 根据id删小节
        eduVideoService.removeVideoByCourseId(courseId);
        // 删章节
        eduChapterService.removeChapterByCourseId(courseId);
        // 删描述
        eduCourseDescriptionService.removeById(courseId);
        // 删除课程
        boolean b = this.removeById(courseId);
        return b;
    }

    @Override
    public List<EduCourse> getHotCourse() {
        List<EduCourse> hotCourse = baseMapper.getHotCourse();

        return hotCourse;

    }
}
